const { carouselInformationList } = require('../../../../api/home');
const { replaceImg } = require('../../../../utils/util');

// packageA/pages/active/detail4/index.js
Page({
  data: {
    detail: {}
  },
  onLoad(options) {
    const { id } = options;
    fetchCarouselInformationList.call(this, id);
  }
})

//  请求对应的轮播图数据
async function fetchCarouselInformationList(id) {
  wx.showLoading({ title: '加载中...' });
  const { errors, result } = await carouselInformationList({ id });
  if(errors.length > 0) { return; }
  
  this.setData({ detail: setResult(result) });
  //  将数据缓存起来
  wx.hideLoading();
}

//  加工数据
function setResult(data) {
  const _data = JSON.parse(JSON.stringify(data));
  let result = {}; 
  if(_data.length) {
    result = _data[0];
    result.imageText = replaceImg(result.imageText);
  }

  return result;
}